package com.prs.crm.action.receipt;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.document.DraftType;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.receipt.ReceiptFlow;
import com.prs.crm.domain.receipt.ReceiptFlowType;
import com.prs.crm.domain.receipt.ReceiptMoudle;
import com.prs.crm.domain.receipt.ReceiptMoudleType;
import com.prs.crm.service.receipt.ReceiptFlowService;
import com.prs.crm.service.receipt.ReceiptFlowTypeService;
import com.prs.crm.service.receipt.ReceiptMoudleService;
import com.prs.crm.service.receipt.ReceiptMoudleTypeService;

public class ReceiptMoudleAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	private ReceiptMoudle receiptMoudle;
	private ReceiptMoudleService receiptMoudleService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private List<ReceiptFlow> receiptFlow;
	private ReceiptFlowService receiptFlowService;
	private List<ReceiptFlowType> receiptFlowType;
	private ReceiptFlowTypeService receiptFlowTypeService;
	private List<ReceiptMoudle> receiptMoudles;
	
	private List<ReceiptMoudleType> receiptMoudleType;
	private ReceiptMoudleTypeService receiptMoudleTypeService;
	
	public List<ReceiptMoudle> getReceiptMoudles() {
		return receiptMoudles;
	}
	public void setReceiptMoudles(List<ReceiptMoudle> receiptMoudles) {
		this.receiptMoudles = receiptMoudles;
	}
	public List<ReceiptMoudleType> getReceiptMoudleType() {
		return receiptMoudleType;
	}
	public void setReceiptMoudleType(List<ReceiptMoudleType> receiptMoudleType) {
		this.receiptMoudleType = receiptMoudleType;
	}
	public ReceiptMoudleTypeService getReceiptMoudleTypeService() {
		return receiptMoudleTypeService;
	}
	public void setReceiptMoudleTypeService(
			ReceiptMoudleTypeService receiptMoudleTypeService) {
		this.receiptMoudleTypeService = receiptMoudleTypeService;
	}
	public ReceiptMoudle getReceiptMoudle() {
		return receiptMoudle;
	}
	public void setReceiptMoudle(ReceiptMoudle receiptMoudle) {
		this.receiptMoudle = receiptMoudle;
	}
	public ReceiptMoudleService getReceiptMoudleService() {
		return receiptMoudleService;
	}
	public void setReceiptMoudleService(ReceiptMoudleService receiptMoudleService) {
		this.receiptMoudleService = receiptMoudleService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public List<ReceiptFlow> getReceiptFlow() {
		return receiptFlow;
	}
	public void setReceiptFlow(List<ReceiptFlow> receiptFlow) {
		this.receiptFlow = receiptFlow;
	}
	public ReceiptFlowService getReceiptFlowService() {
		return receiptFlowService;
	}
	public void setReceiptFlowService(ReceiptFlowService receiptFlowService) {
		this.receiptFlowService = receiptFlowService;
	}
	public List<ReceiptFlowType> getReceiptFlowType() {
		return receiptFlowType;
	}
	public void setReceiptFlowType(List<ReceiptFlowType> receiptFlowType) {
		this.receiptFlowType = receiptFlowType;
	}
	public ReceiptFlowTypeService getReceiptFlowTypeService() {
		return receiptFlowTypeService;
	}
	public void setReceiptFlowTypeService(
			ReceiptFlowTypeService receiptFlowTypeService) {
		this.receiptFlowTypeService = receiptFlowTypeService;
	}
	@Action("listReceiptMoudle")
	public String listReceiptMoudle(){
		Person per=AppContext.getAccountContext().currentUser();
		List<ReceiptMoudle> dt=this.getReceiptMoudleService().getAllReceiptMoudle();
		for (ReceiptMoudle dts : dt) {
			if (dts.getMoudlePerson()==null) {
				dts.setUpState("5");
				this.getReceiptMoudleService().submit(dts);
			}else{
				String[] wh=dts.getMoudlePerson().split(",");
				for (String whs : wh) {
					if (per.getResume().getName().equals(whs)) {
						dts.setUpState(per.getId()+":1");
						this.getReceiptMoudleService().submit(dts);
						break;
					}else{
						dts.setUpState("5");
						this.getReceiptMoudleService().submit(dts);
					}
				}
			}
		}
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(ReceiptMoudle.class);
		criteria.add(Restrictions.eq("upState", per.getId()+":1"));
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getReceiptMoudleService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("editReceiptMoudle")
	public String editReceiptMoudle(){
		this.receiptMoudleType=this.getReceiptMoudleTypeService().getAllReceiptMoudleType();
		return SUCCESS;
	}
	
	@Action("editReceiptMoudleFlow")
	public String editReceiptMoudleFlow(){
		this.receiptFlow=this.receiptFlowService.getAllReceiptFlow();
		this.receiptFlowType=this.receiptFlowTypeService.getAllreceiptFlowType();
		return SUCCESS;
	}
	@Action(value="saveReceiptMoudle",results={
			@Result(name="success",type="redirect",location="listReceiptMoudle"),
			@Result(name="error",type="chain",location="editReceiptMoudle")
	})
	public String saveReceiptMoudle(){
		if (receiptMoudle.getMoudlePerson()==null || "".equals(receiptMoudle.getMoudlePerson().trim()) || "0".equals(receiptMoudle.getMoudlePerson().trim())) {
			receiptMoudle.setMoudlePerson(AppContext.getAccountContext().currentUser().getResume().getName());
			receiptMoudle.setMoudlePersonId(AppContext.getAccountContext().currentUser().getId()+"");
		}
		if (receiptMoudle.getHoutai()==null || "".equals(receiptMoudle.getHoutai())) {
			this.addActionError("后台触发程序不能为空！");
			return ERROR;
		}
		this.getReceiptMoudleService().save(receiptMoudle);
		return SUCCESS;
	}
	@Action(value="removeReceiptMoudle",results={
			@Result(name="success",type="redirect",location="listReceiptMoudle"),
			@Result(name="error",type="chain",location="editReceiptMoudle")
	})
	public String removeReceiptMoudle(){
		if (ids==null) {
			this.addActionError("请选择一行！");
			return ERROR;
		}
		this.getReceiptMoudleService().batchRemove(ids);
		return SUCCESS;
	}
	@Action("registration")
	public String registration(){
		receiptMoudleType=this.getReceiptMoudleTypeService().getReceiptMoudleType();
		receiptMoudles=this.getReceiptMoudleService().getReceiptMoudle();
		return SUCCESS;
	}
}
